package com.myfum.wingplayer.utils
{
	import com.myfum.wingplayer.Config;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	
	public class FileManager extends EventDispatcher
	{
		public function FileManager(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		private var _dic:Dictionary;
		
		/**
		 * 获取本地文件dictionary
		 */
//		public function get localFile():Dictionary{
//			if(_dic == null){
//				return scan();
//			}else{
//				return _dic;
//			}
//		}
		
		/**
		 * 扫描指定目录
		 * @param folder path
		 * @return array
		 */
		public function scan(folder:String):Array{
			var dir:File = new File(folder);
			if(!dir.exists){
				dir.createDirectory();
			}
			return dir.getDirectoryListing();
		}
		
		/**
		 * 获取program列表
		 * @return array contain all program file
		 * 
		 */		
		public function getProgramFileList():Array{
			return scan(File.applicationStorageDirectory.resolvePath(Config.PROGRAM_FOLDER).nativePath);
		}
		
		public function getProgramFile(name:String):File{
			return File.applicationStorageDirectory.resolvePath(Config.PROGRAM_FOLDER + name);
		}
		
		/**
		 * 获取schedule列表
		 * @return array contain 7 days schedule
		 * 
		 */		
		public function getScheduleFileList():Array{
			var arr:Array = scan(File.applicationStorageDirectory.resolvePath(Config.SCHEDULE_FOLDER).nativePath);
			for(var i:int = arr.length;i<7;i++){
				var f:File = File.applicationStorageDirectory.resolvePath(
					Config.SCHEDULE_FOLDER + 
					"mfmschedule_" + 
					i +
					".mfs"
				);
				if(!f.exists){
					f = createEmptyFile(f);
				}
				arr.push(f);
			}
			return arr;
		}
		
		/**
		 * 获取特定天的schedule文件
		 * @param day 天，从0到6
		 * @return File
		 * 
		 */		
		public function getScheduleByDay(day:int):File{
			var f:File = File.applicationStorageDirectory.resolvePath(
				Config.SCHEDULE_FOLDER + 
				"mfmschedule_" + 
				day +
				".mfs"
			);
			f = new File(f.nativePath.replace("WingPlayer", "MFManager"));
			if(!f.exists){
				f = createEmptyFile(f);
			}
			return f;
		}
		
		/**
		 * 判断一个文件是否已存在于本地路径
		 */
		public function hasFile(src:String):Boolean{
			var file:File = new File(File.applicationStorageDirectory.resolvePath(src).nativePath);
			return file.exists;
		}
		
		/**
		 * 将字符串同步写回文件中
		 * @param file path
		 * @param file content
		 * @return true or flase
		 */
		public function writeFile(file:File, content:String):Boolean{
			try{
				var fileStream:FileStream = new FileStream();
				fileStream.open(file, FileMode.WRITE);
				fileStream.position = 0;
				fileStream.truncate();
				fileStream.writeUTFBytes(content);
				fileStream.close();
			}catch(e:Error){
				Alert.show("Write file fault:"+e.message);
				return false;
			}
			return true;
		}
		
		/**
		 * 删除一个文件，成功返回true， 失败返回false
		 * @param file 要删除的File对象
		 * @return 删除成功失败标志
		 * 
		 */		
		public function deleteFile(file:File):Boolean{
			if(file == null){
				return false;
			}
			var result:Boolean;
			try{
				file.deleteFile();
				result = true;
			}catch(e:Error){
				result = false;
			}
			return result;
		}
		
		/**
		 * 创建一个空文件
		 * @param file
		 * @return 
		 * 
		 */		
		public function createEmptyFile(file:File):File{
			var content:String = '<?xml version="1.0" encoding="utf-8"?><root/>';
			writeFile(file, content);
			return file;
		}
	}
}










